<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
String path = request.getContextPath(); 
String strUserId = request.getSession().getAttribute("stxtUName").toString();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
  <head>
  	<style type="text/css" media="all">   
	    .allow-float {clear:none!important;} /* 允许该元素浮动 */  
	    .stop-float {clear:both!important;} /* 阻止该元素浮动 */  
	   	.icon-first {float:left;}   
	   	.icon-choosed {float:left;padding:5 0 0 5px;}
	    .icon-second {float:left;padding:0 0 0 20px;} 
	    .win-first {float:left;}   
	    .win-second {float:left;padding:0 0 0 20px;}   
	   	.standby-yes {float:left;}   
	    .standby-no {float:left;padding:0 0 0 20px;}   
	    .age-field {float:left;padding:0 0 0 58px;*padding:0 0 0 50px!important;*padding:0 0 0 50px;}   
	</style> 
  
    <base href="<%=basePath%>"> 
    <link rel="stylesheet" type="text/css" href="<%=path%>/js/ext/resources/css/ext-all.css">  
	<link rel="stylesheet" type="text/css" href="<%=path%>/js/ext/resources/css/edit-grid.css">  
	<script type="text/javascript" src="<%=path%>/js/ext/adapter/ext/ext-base.js"></script>
	<script type="text/javascript" src="<%=path%>/js/ext/ext-all.js"></script>
	<script type="text/javascript" src="<%=path%>/js/ext/DWRTreeLoader.js"></script> 
	<script type="text/javascript" src="<%=path%>/js/ext/query-dwr-proxy.js"></script>	
	<script type="text/javascript" src="<%=path%>/js/ext/ext-lang-zh_CN.js"></script>
	<script type="text/javascript" src="<%=path%>/js/system/TreeCheckNode.js"></script>
	<script type='text/javascript' src='/btsm/dwr/interface/JSystemWeb.js'></script>
	<script type='text/javascript' src='/btsm/dwr/engine.js'></script>
	<script type='text/javascript' src='/btsm/dwr/util.js'></script>	 
  </head>
<body> 	
	<div id="roleTreeDiv" style="height:80%"/>
	<div id="menuTreeContent" style="height:100%"/>   
<script type="text/javascript">


var json = [];


Ext.onReady(function(){
	var viewport = new Ext.Viewport({
		layout:'border',
		frame:true,
		items:[
			{
				region: 'west',
				contentEl: 'roleTreeDiv',
				margins:'110 5 30 5',
				split: true,
				border: true,
				collapsible: true,
				width: 400
			},
			{
				region: 'center',
				contentEl: 'menuTreeContent',
				margins:'110 30 30 5',
				split: true,
				border: true	
			}]
		});
		
	var roleTreeDiv = Ext.get("roleTreeDiv");	   
    var roleRoot = new Ext.tree.AsyncTreeNode({
           text: '系统角色', 
		   icon:'/btsm/js/ext/resources/images/user.gif',
           draggable:false, 
           id:'0'
     });	 
	var roleTreePanel = new Ext.tree.TreePanel( {
		el : "roleTreeDiv",
		border : false,
		animate : true,
		autoHeight : true,
		height:roleTreeDiv.getComputedHeight(),
		rootVisible : true,
		autoScroll : true,
		containerScroll : true,
		loader : new Ext.tree.DWRTreeLoader( {
			dwrCall:JSystemWeb.findRoleTree,queryParam:{}
		}),
		root : roleRoot
	});
	roleTreePanel.render();
	roleRoot.expand(true,true);	
 
	var saveButton = new Ext.Button(
		{
			text:'保存', 
			pressed:true,
			id:'saveButton',
			disabled:true,
			handler:saveMenuForRole
		});
	function saveMenuForRole()
	{
		var menuNodeCode = "";	
		var checkedNodes = menuTree.getChecked();			
		for(var i=0;i<checkedNodes.length;i++)
		{
			var node = checkedNodes[i];
			if(menuNodeCode == "")
			{
				menuNodeCode = node.id;
			}else
			{
				menuNodeCode = menuNodeCode +";"+ node.id;
			}
		}
		var roleNode = roleTreePanel.getSelectionModel().getSelectedNode();
		var strRoleId = roleNode.id;
		JSystemWeb.save(menuNodeCode,strRoleId,function(value)
		{
			if(value)
			{
				Ext.Msg.alert('提示',"权限设置成功!");
			}else{
				Ext.Msg.alert('提示',"权限设置失败!");
			}
			var firstNode = menuRoot.firstChild;
			while(firstNode!=null)
			{
				firstNode.remove();
				firstNode = menuRoot.firstChild;
			}
		})
		
	}
	
	
	
	var menuTreeDiv = Ext.get("menuTreeContent");	
	var Tree = Ext.tree;    
    var menuRoot = new Tree.AsyncTreeNode({
           text: '顶部菜单', 
		   icon:'/btsm/js/ext/resources/images/user.gif',
           draggable:false, 
           id:'0'
     });
	
	var menuLoader =  new Ext.tree.DWRTreeLoader( {
						dwrCall:JSystemWeb.getMenuTreeForAuth,queryParam:{ROLE_ID:0}
					});
	var menuTree = new Ext.tree.TreePanel( {
		el : "menuTreeContent", 
		height:menuTreeDiv.getComputedHeight(), 
		loader : menuLoader, 
		checkModel:'cascade',   //对树的级联多选 
		containerScroll: true,
		autoScroll:true,
		enableDD:false,
		enableDrag:false,
		enableDrop:false,
		rootVisible:false,
		header:true, 
		selModel:new Ext.tree.CheckNodeMultiSelectionModel(),
		tbar: new Ext.Toolbar( [saveButton]),
		dropConfig: {appendOnly:true},
		toggleCheck : function(value){
			var cb = this.checkbox;
			if(cb){
				cb.checked = (value === undefined ? !cb.checked : value);
			}
		},
		getChecked : function(a, startNode){
			startNode = startNode || this.root;
			var r = [];
			var f = function(){
				if(this.attributes.checked){
					r.push(!a ? this : (a == 'id' ? this.id : this.attributes[a]));
				}
			}
			startNode.cascade(f);
			return r;
		}
	});
	menuTree.setRootNode(menuRoot);
	menuTree.render();
	//menuRoot.expand(true,true);	
	
	roleTreePanel.on('click', function (node)
	{
		var firstNode = menuRoot.firstChild;
		while(firstNode!=null)
		{
			firstNode.remove();
			firstNode = menuRoot.firstChild;
		}
		saveButton.enable();
		if(!node.firstChild)
		{
			menuTree.on("beforeload", function(menuNode) { 
			  menuTree.loader.queryParam.ROLE_ID = node.id;
			});
		
			var n = new Array();  
			JSystemWeb.findMenuTreeByParentID(node.id,function(value)
			{
				for(var i = 0; i < value.length; i++)
				{
					n[i] = new Ext.tree.AsyncTreeNode(value[i]);
				}
				
				menuRoot.appendChild(n);
				menuRoot.expand();
				
				var aryAllNodes = menuRoot.childNodes;	
				for(var i=0; i<aryAllNodes.length; i++)//展开所有菜单树
				{
					var node = aryAllNodes[i];
					if (!node.isLoaded())
					{
						node.expand(true, false);            			
					} 
				}
				
			});
		}
	})
})

</script>
	
</body>
</html>